package com.alibaba.nacos;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import cn.hutool.setting.yaml.YamlUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/5/7
 */
@SpringBootApplication(scanBasePackages = "com.alibaba.nacos")
@ServletComponentScan
@EnableScheduling
public class DDNacosApplication {

    public static final String NACOS = "/nacos";

    public static void main(String[] args) {
        // 程序还未启动，获取application文件中的值
        Dict application = YamlUtil.loadByPath("application.yml");

        // 是否单机模式启动
        String standalone = StrUtil.toStringOrNull(application.getByPath("nacos.standalone"));
        // 是否开启鉴权
        String enabled = StrUtil.toStringOrNull(application.getByPath("nacos.core.auth.enabled"));
        // 端口
        String port = StrUtil.toStringOrNull(application.getByPath("server.port"));
        // tomcat目录
        String basedir = StrUtil.toStringOrNull(application.getByPath("server.tomcat.basedir"));

        System.setProperty("nacos.standalone", StrUtil.isNotBlank(standalone) ? standalone : "true");
        System.setProperty("nacos.core.auth.enabled", StrUtil.isNotBlank(enabled) ? enabled : "true");
        System.setProperty("server.tomcat.basedir", StrUtil.isNotBlank(basedir) ? basedir : "log");
        //自定义启动端口号
        System.setProperty("server.port", StrUtil.isNotBlank(port) ? port : "8848");

        SpringApplication.run(DDNacosApplication.class, args);

        System.out.println(StrUtil.format("\n----------------------------------------------------------\n\t" +
                        "启动地址: \t{} \n\t" +
                        "项目启动成功！\n\t" +
                        "用户登录信息: \t user:【{}】\t pass:【{}】 \n" +
                        "----------------------------------------------------------",
                StrUtil.format("http://localhost:{}/nacos", port),
                "nacos",
                "nacos"));
    }

    /**
     * 默认跳转首页
     *
     * @return 跳转首页
     */
    @GetMapping("/")
    public String index() {
        // 视图重定向 - 跳转
        return NACOS;
    }

}
